#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/11/2 15:11
# @Author  : 胡浩浩
# @File    : group_api.py
# @IDE     : PyCharm
# @description :
from apps.models.group_model import GroupModel, Group

from fastapi import APIRouter

group_router = APIRouter()


@group_router.get("/groups")
async def get_group():
    groups = await GroupModel.all()
    group_list = [[g.product_name,g.group_info] for g in groups]

    return group_list
@group_router.get("/group_info")
async def get_group_info(product_name:str):
    group = await GroupModel.filter(product_name=product_name).first()
    if group:
        return group

# @group_router.delete("/groups")
# async def insert_group(group: Group):
#     print(group.product_name)
#     select_group = await GroupModel.filter(product_name=group.product_name,
#                                                     group_name=group.group_name,
#                                                     api_list=group.api_list).first()
#     if select_group:
#         await select_group.delete()
#         return {"message": f"产品{group.swagger_name}对应的测试单已删除"}
#     else:
#         return {"message": f"产品{group.swagger_name}对应的测试单不存在"}


@group_router.post("/groups")
async def insert_group(group: Group):
    print(group.product_name)
    select_group = await GroupModel.filter(product_name=group.product_name).first()
    print("2222",group.group_info)
    if select_group:
        # 直接进行更新操作
        select_group.product_name=group.product_name
        select_group.group_info=group.group_info
        await select_group.save(update_fields=["product_name","group_info"])
        return {"message": f"产品{group.product_name}对应的模块分组配置已更新"}
    else:
        get_group = GroupModel(product_name=group.product_name,group_info=group.group_info)
        await get_group.save()
        return {"message": f"产品{group.product_name}对应的模块分组新增成功", "data": get_group}
